package com.lightkits.wms.domain.purchasereceive.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.db.entity.FndOrganizationTl;
import com.lightkits.common.db.entity.FndPartyBase;
import com.lightkits.common.db.entity.FndPartyTl;
import com.lightkits.common.db.entity.ScmPoHeader;
import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.logging.Logger;
import com.lightkits.framework.common.logging.LoggerFactory;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.domain.purchasereceive.service.PurHeadDomain;
import com.lightkits.wms.domain.stocktransfer.service.impl.SubStockTransferDomainImpl;
import com.lightkits.wms.dto.purchasereceive.models.PoHeadDTO;
import com.lightkits.wms.util.DomainUtil;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * PurHeadDomainImpl
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/4/25 22:15
 */
@Service
public class PurHeadDomainImpl implements PurHeadDomain {

    private static final Logger LOGGER = LoggerFactory.getLogger(SubStockTransferDomainImpl.class);

    @Override
    public PoHeadDTO getHead(String poId) {
        PoHeadDTO dto = new PoHeadDTO();


        ScmPoHeader header = new ScmPoHeader().selectById(poId);
        Map<String, String> sourceTypeSelect = DomainUtil.getSysCodeMeaning("SCM_PO_STATUS");

        if (header != null) {
            dto.setPoId(header.getId().toString());
            dto.setPoNum(header.getPoNum());
            dto.setPoStatus(sourceTypeSelect.get(header.getPoStatus()));
            dto.setMeouId(header.getReceiveOrgId().toString());
            FndOrganizationTl tl = new FndOrganizationTl()
                    .selectOne(new EntityWrapper()
                            .eq(FndOrganizationTl.ORGANIZATION_ID, header.getReceiveOrgId()));
            if (!ObjectUtil.isEmpty(tl)) {
                dto.setMeouName(tl.getOrganizationName());
            }
            dto.setSupplierId(header.getSupplierId().toString());
            if (!ObjectUtil.isEmpty(header.getSupplierSiteId())) {
                dto.setSupplierSiteId(header.getSupplierSiteId().toString());
            }
            FndPartyBase base = new FndPartyBase().selectById(header.getSupplierId());
            FndPartyTl partyTl = new FndPartyTl().selectOne(new EntityWrapper()
                    .eq(FndPartyTl.PARTY_ID, header.getSupplierId()));
            if (partyTl != null) {
                dto.setSupplierName(partyTl.getPartyName());
            }
            if (base != null) {
                dto.setAddress(base.getAddress());
                dto.setPhoneNum(base.getPhoneNumber());
                dto.setContact(base.getContact());
            }

        }

        return dto;
    }
}
